<template>
  <view class="initiated">
    <!-- 标签栏 -->
    <view class="tab-bar">
      <view
        v-for="(item, index) in tabList"
        :key="index"
        class="tab-item"
        :class="{ active: currentTab === index }"
        @click="switchTab(index)"
      >
        <text>{{ item }}</text>
      </view>
    </view>

    <!-- 合同列表 -->
    <view class="contract-list">
      <view
        v-for="(contract, contractIndex) in filteredContractList"
        :key="contractIndex"
        class="contract-item"
        @click="goToDetail(contract, contractIndex)"
      >
        <view class="contract-header">
          <img
            class="contract-icon"
            src="../../static/contract.png"
          />
          <view class="contract-info">
            <text class="contract-name">{{ contract.name }}</text>
            <text class="contract-time">起止时间:{{ contract.time }}</text>
          </view>
          <img
            class="status-stamp"
            :src="contract.status === '未签署' ? '../../static/not-signed.png' : '../../static/signed.png'"
          />
        </view>
        <view class="contract-footer">
          <img class="avatar" src="../../static/logo.png" />
          <text class="initiator">发起人</text>
          <text
            v-if="contract.status === '未签署'"
            class="sign-btn"
            @click.stop="goSign(contract)"
          >去签署</text>
        </view>
      </view>
      <view class="no-more">没有更多了</view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'

const tabList = ref(['全部', '未签署', '已签署'])
const currentTab = ref(0)
const contractList = ref([
  {
    name: '软件开发合同',
    time: '2025/01/01 - 2025/06/30',
    status: '未签署'
  },
  {
    name: '咨询服务合同',
    time: '2025/01/15 - 2025/12/31',
    status: '已签署'
  },
  {
    name: '产品代理合同',
    time: '2025/02/01 - 2025/12/31',
    status: '未签署'
  },
  {
    name: '培训服务合同',
    time: '2025/01/20 - 2025/03/31',
    status: '已签署'
  },
  {
    name: '市场推广合同',
    time: '2025/02/15 - 2025/08/31',
    status: '未签署'
  },
  {
    name: '技术支持合同',
    time: '2025/01/10 - 2025/12/31',
    status: '已签署'
  },
  {
    name: '数据服务合同',
    time: '2025/03/01 - 2025/12/31',
    status: '未签署'
  },
  {
    name: '维护服务合同',
    time: '2025/01/05 - 2025/12/31',
    status: '已签署'
  }
])

// 根据当前标签筛选合同列表
const filteredContractList = computed(() => {
  if (currentTab.value === 0) {
    return contractList.value // 全部
  } else if (currentTab.value === 1) {
    return contractList.value.filter(contract => contract.status === '未签署') // 未签署
  } else {
    return contractList.value.filter(contract => contract.status === '已签署') // 已签署
  }
})

function switchTab(index) {
  currentTab.value = index
}

function goSign(contract) {
  // 跳转到接收详情页，传递合同参数
  const index = contractList.value.findIndex(item => item.name === contract.name && item.time === contract.time)
  uni.navigateTo({
    url: `/pages/received_detail/index?id=${index}&name=${encodeURIComponent(contract.name)}&time=${encodeURIComponent(contract.time)}&status=${encodeURIComponent(contract.status)}`
  })
}

function goToDetail(contract, index) {
  uni.navigateTo({
    url: `/pages/received_detail/index?id=${index}&name=${encodeURIComponent(contract.name)}&time=${encodeURIComponent(contract.time)}&status=${encodeURIComponent(contract.status)}`
  })
}
</script>

<style scoped>
.initiated {
  background-color: #fff;
  min-height: calc(100vh - 100rpx);
  padding-bottom: 100rpx;
}

.tab-bar {
  display: flex;
  flex-direction: row;
  height: 80rpx;
  line-height: 80rpx;
  border-bottom: 1px solid #f5f5f5;
}

.tab-item {
  flex: 1;
  text-align: center;
  font-size: 28rpx;
  color: #333;
}

.tab-item.active {
  color: #ff0000;
  border-bottom: 2rpx solid #ff0000;
}

.contract-list {
  padding: 10px;
}

.contract-info {
  display: flex;
  flex-direction: column;
}

.contract-item {
  background-color: #fff;
  border-radius: 16rpx;
  box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);
  margin-bottom: 20rpx;
  padding: 20rpx;
  cursor: pointer;
  transition: all 0.3s ease;
}

.contract-item:active {
  transform: scale(0.98);
  box-shadow: 0 0 5rpx rgba(0, 0, 0, 0.2);
}

.contract-header {
  display: flex;
  align-items: center;
}

.contract-icon {
  width: 90rpx;
  height: 90rpx;
  margin-right: 20rpx;
}

.contract-name {
  font-size: 34rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.contract-time {
  font-size: 24rpx;
  color: #999;
}

.status-stamp {
  width: 120rpx;
  height: 135rpx;
  position: absolute;
  right: 20rpx;
}

.contract-footer {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-top: 20rpx;
}

.avatar {
  width: 60rpx;
  height: 60rpx;
  border-radius: 10rpx;
  margin: 0 10rpx 0 10rpx;
}

.initiator {
  font-size: 28rpx;
  margin-right: auto;
}

.sign-btn {
  color: red;
  font-size: 28rpx;
}

.no-more {
  text-align: center;
  color: #999;
  padding: 40rpx 0;
}
</style>